<template>
  <div id="Sku" v-loading="loading">
    <v-tableTitle
      :btnConfig="btnConfig"
      :tableSelectData="tableSelectData"
      :title="$t('bill.goods')"
      @del="del"
      @newCreate="newCreate"
      @exportAsn="exportAsn"
      @handleDownErrorFile="exceptionFileUrl"
      @handleDownTemplate="downloadSkuExcelFile"
    ></v-tableTitle>
    <v-table
      :pageSize="pageSize"
      :page="page"
      :tableConfig="tableConfig"
      :tableData="tableData"
      :total="total"
      @findData="getTableData"
      @getTableData="getTableData"
      @searhData="searhData"
      @selectData="selectData"
      @setdialog="setdialog"
      @upData="getTableData"
      ref="table"
    ></v-table>
    <el-dialog
      :modal="false"
      :visible.sync="dialogFlag"
      title=""
      top="0"
      v-dialogDrag
      :close-on-click-modal="false"
      width="900px"
    >
      <router-view></router-view>
    </el-dialog>
  </div>
</template>

<script type="text/ecmascript-6">
import tableTitle from "commons/TableTitle";
import table from "commons/Table";
import baseMixin from "@/mixins/BaseMixin";
import config from "./Config";

export default {
  name: "sku",
  mixins: [baseMixin, config],
  data() {
    return {
      loading: false,
      tableData: [],
      pageSize: 10,
      page: 1,
      total: 10,
      tableSelectData: [],
      tableSearhData: {},
      dialogFlag: false,
    };
  },
  computed: {
    btnConfig() {
      if (this.$store.state.wms.toUpperCase().includes('WMWHSE')) {
        return [
          {label: this.$t('common.create'), certigier: this.newStatus||(!this.btnPermNameList.includes('EN020101')&&!this.btnPermNameList.includes('WM030101')), invalid: "newCreate"},
          {type: "file", certigier: this.poiStatus||(!this.btnPermNameList.includes('EN020103')&&!this.btnPermNameList.includes('WM030103')), label: this.$t('common.import'), invalid: "file",importType:10301,importUrl:"importSku1",downTemplate:"handleDownTemplate"},
          // {label: this.$t('config.importTemDownload'), certigier: this.poiStatus||(!this.btnPermNameList.includes('EN020104')&&!this.btnPermNameList.includes('WM030104')), invalid: "downloadSkuExcelFile"},
          {label: this.$t('common.export'), certigier: this.exportstatus||(!this.btnPermNameList.includes('EN020105')&&!this.btnPermNameList.includes('WM030105')), disabled: true, invalid: "exportAsn"},
        ]
      } else {
        return [
          {label: this.$t('common.create'), certigier: this.newStatus||(!this.btnPermNameList.includes('EN020101')&&!this.btnPermNameList.includes('WM030101')), invalid: "newCreate"},
          {label: this.$t('common.delete'), certigier: this.deleteStatus||(!this.btnPermNameList.includes('EN020102')&&!this.btnPermNameList.includes('WM030102')), disabled: true, invalid: "del"},
          {type: "file", certigier: this.poiStatus||(!this.btnPermNameList.includes('EN020103')&&!this.btnPermNameList.includes('WM030103')), label: this.$t('common.import'), invalid: "file",importType:10301,importUrl:"importSku1",downTemplate:"handleDownTemplate",},
          // {label: this.$t('config.importTemDownload'), certigier: this.poiStatus||(!this.btnPermNameList.includes('EN020104')&&!this.btnPermNameList.includes('WM030104')), invalid: "downloadSkuExcelFile"},
          {label: this.$t('common.export'), certigier: this.exportstatus||(!this.btnPermNameList.includes('EN020105')&&!this.btnPermNameList.includes('WM030105')), disabled: true, invalid: "exportAsn"},
        ]
      }

    },
    tableConfig() {
      if (this.$store.state.wms.toUpperCase().includes('WMWHSE')) {
        return [
          {
            label: this.$t('config.sku'),
            prop: "sku",
            input: "input",
          },
          {
            label: this.$t('common.desc'),
            prop: "descr",
            input: "input"
          },
          {
            label: this.$t('config.shipper'),
            prop: "storerkey",
            input: "input",
          },
          {
            label: this.$t('config.packaging'),
            prop: "packkey",
            input: "input"
          },
          {
            label: this.$t('config.boxGroup'),
            prop: "cartongroup",
            input: "input",
            //options: this.getOptions('queryCartonizationDropDown')
          },
          {
            label: this.$t('config.allocaStrategy'),
            prop: "strategykey",
            input: "input",
            //options: this.getOptions('queryAllocatestrategyDropDown')
          },
          {
            label: this.$t('config.putawayStrategy'),
            prop: "putawaystrategykey",
            input: "input",
            //options: this.getOptions('queryPutawaystrategyDropDown')
          },
          {
            label: this.$t('config.isQuality'),
            prop: "serialnumbe",
            input: "select",
            options: [
              {code: '0', description: this.$t('code.no')},
              {code: '1', description: this.$t('code.yes')}
            ],
            disabled: this.whseidflag
          },
          {
            label: this.$t('config.batchValidate'),
            prop: "lottablevalidationkey",
            input: "input",
            //options: this.getOptions('queryLottablevalidationDropDown')
          },
          {
            label: this.$t('config.area'),
            prop: "putawayzone",
            input: "input",
            //options: this.getOptions('queryPutawayzoneDropDown')
          },
        ]
      } else {
        return [
          {
            label: this.$t('config.sku'),
            prop: "sku",
            input: "input",
          },
          {
            label: this.$t('common.desc'),
            prop: "descr",
            input: "input"
          },
          {
            label: this.$t('config.shipper'),
            prop: "storerkey",
            input: "input",
          },
          {
            label: this.$t('config.packaging'),
            prop: "packkey",
            input: "input"
          },
          {
            label: this.$t('config.boxGroup'),
            prop: "cartongroup",
            input: "input",
            //options: this.getOptions('queryCartonizationDropDown')
          },
          {
            label: this.$t('config.putawayStrategy'),
            prop: "putawaystrategykey",
            input: "input",
            //options: this.getOptions('queryPutawaystrategyDropDown')
          },
          {
            label: this.$t('config.isQuality'),
            prop: "serialnumbe",
            input: "select",
            options: [
              {code: '0', description: this.$t('code.no')},
              {code: '1', description: this.$t('code.yes')}
            ],
            disabled: this.whseidflag
          },
          {
            label: this.$t('config.batchValidate'),
            prop: "lottablevalidationkey",
            input: "input",
            //options: this.getOptions('queryLottablevalidationDropDown')
          },
          {
            label: this.$t('config.area'),
            prop: "putawayzone",
            input: "input",
            //options: this.getOptions('queryPutawayzoneDropDown')
          },
        ]
      }
    }
  },
  created() {
  },
  methods: {

    exceptionFileUrl(exceptionUrl) {

      console.log("exceptionFileUrl = %o", 1);
      const data = [{
        exceptionFileUrl: exceptionUrl
      }];

      // 异常下载配置
      let urlConf = {responseType: 'blob'};
      let exceptionFileName = this.$t('config.exceptionFile') + ".xls";
      let url = '/comExcelImportResult/downloadExceptionFile';
      let options = {type: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document;charset=utf-8'};

      try {
        this.$api.post(url, data, urlConf).then(res => {
          let blobParts = [res.data];
          if (window.navigator.msSaveBlob) {
            const blobObject = new Blob(blobParts);
            window.navigator.msSaveBlob(blobObject, exceptionFileName);
          } else {
            let tagName = 'a';
            const blob = new Blob(blobParts, options);
            const downloadElement = document.createElement(tagName);
            // 创建下载的链接
            const href = window.URL.createObjectURL(blob);
            downloadElement.style.display = 'none';
            downloadElement.href = href;
            // 下载后文件名
            downloadElement.download = exceptionFileName;
            document.body.appendChild(downloadElement);
            // 点击下载
            downloadElement.click();
            // 下载完成移除元素
            document.body.removeChild(downloadElement);
            // 释放掉blob对象
            window.URL.revokeObjectURL(href);
          }
          this.importVisualDialogFlag = false;
        });
      } catch (e) {
        this.$message.error(e);
      } finally {
        
      }
    },
    newCreate() {
      this.$store.commit("setNewFlag", true);
      // this.$router.push({name: 'skuDeawer'});
      this.dialogFlag = true;
      setTimeout(() => {
        this.$router.push({name: 'skuDeawer'});
      }, 0)
    },

    async downloadSkuExcelFile(data) {
      try {
        let res = await this.$api.post('/downloadSkuExcelFile', data, {responseType: 'blob'})
        if (window.navigator.msSaveBlob) {
          var blobObject = new Blob([res.data]);
          window.navigator.msSaveBlob(blobObject, this.$t('config.goodsInfoImportTem'));
        } else {
          var blob = new Blob([res.data], {type: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document;charset=utf-8'});
          var filename = this.$t('config.goodsInfoImportTem');
          var downloadElement = document.createElement('a');
          var href = window.URL.createObjectURL(blob); //创建下载的链接
          downloadElement.style.display = 'none';
          downloadElement.href = href;
          downloadElement.download = filename; //下载后文件名
          document.body.appendChild(downloadElement);
          downloadElement.click(); //点击下载
          document.body.removeChild(downloadElement); //下载完成移除元素
          window.URL.revokeObjectURL(href); //释放掉blob对象
        }
      } catch (e) {
        this.$message.error(e);
      } finally {
        
      }
    },
    async exportAsn(data) {
      try {
        let res = await this.$api.post('/exportSku', data, {responseType: 'blob'})
        if (window.navigator.msSaveBlob) {
          var blobObject = new Blob([res.data]);
          window.navigator.msSaveBlob(blobObject, this.$t('config.goodsInfo'));
        } else {
          var blob = new Blob([res.data], {type: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document;charset=utf-8'});
          var filename = this.$t('config.goodsInfo');
          var downloadElement = document.createElement('a');
          var href = window.URL.createObjectURL(blob); //创建下载的链接
          downloadElement.style.display = 'none';
          downloadElement.href = href;
          downloadElement.download = filename; //下载后文件名
          document.body.appendChild(downloadElement);
          downloadElement.click(); //点击下载
          document.body.removeChild(downloadElement); //下载完成移除元素
          window.URL.revokeObjectURL(href); //释放掉blob对象
        }
      } catch (e) {
        this.$message.error(e);
      } finally {
        
      }
    },
    async del(data) {
      if (!this.$store.state.wms.includes('wmwhse')) {
        try {
          let res = await this.$api.post(`/deleteSku/${this.$store.state.wms}`, data);
          
            this.$message.success(res.returnMgr);
            let page = sessionStorage.getItem('page');
            this.getTableData(page, this.tableSearhData);
        } catch (e) {
          this.$notify({
              title: this.$t('common.error'),
              message: e.returnMgr,
              duration: 0
            });
        }
      } else {
        this.$message.error(this.$t('msg.msg5'))
      }

    },
    //选中的内容
    selectData(data) {
      this.tableSelectData = data;
    },
    async getTableData(page, data) {
      try {
        this.loading = true;
        let res = await this.$api.post(`/querySku/${page}/${this.$store.state.wms}`, data);
        
          this.tableData = res.page.list;
          this.page = res.page.curpage;
          this.pageSize = res.page.pageSize;
          this.total = res.page.total;
        
      } catch (e) {
        this.$message.error(error);
      } finally {
        this.loading = false;
      }
    },
    //搜索的值
    searhData(data, up) {
      if (!up) {
        this.tableSearhData = data;
      } else {
        this.tableSearhData = data;
        this.getTableData(1, this.tableSearhData);
      }
    },
    //设置弹框
    setdialog(data) {
      this.$store.commit("setRouteData", data);
      this.dialogFlag = true;
      setTimeout(() => {
        this.$router.push({name: 'skuDeawer'});
      }, 0)

    },
  },
  mounted() {
    ;
    if (this.$store.state.wms.toUpperCase().includes('WMWHSE')) {
      //仓库级
      this.permissionStatus = false;
      this.saveStatus = this.deleteStatus = this.newStatus = this.poiStatus = this.exportstatus = true;
    }
    this.$store.commit("setSkip", {});
    this.$store.commit("setNewFlag", false);
    this.$store.commit("setRouteData", {});
    this.getTableData(1, {});
  },
  watch: {
    tableSelectData(data) {
      if (this.permissionStatus) {
        if (data.length > 0) {
          this.deleteStatus = this.exportstatus = false;
        } else {
          this.deleteStatus = this.exportstatus = true;
        }
      }
    },
    $route(data) {
      if (data.name === "sku") {
        this.dialogFlag = false;
      }
    },
    dialogFlag(data) {
      if (data === false) {
        this.$store.commit("setNewFlag", false);
        this.getTableData(1, {});
        this.$router.push({name: "sku"});
        this.$store.commit("setSkip", {});
        this.$store.commit("setRouteData", {});
      }
    }
  },
  components: {
    "v-tableTitle": tableTitle,
    "v-table": table,
  }
};
</script>

<style scoped>
#Sku {
  width: 100%;
  height: 99%;
  padding-top: 6px;
}
</style>
